import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syria_sports/presentation/exercises/exercises_page/models/exercise.dart';

import '../../../core/utils/show_toast.dart';
import '../../../data/controllers/notes_controller.dart';
import '../models/note_model.dart';

part 'coach_notes_state.dart';

class CoachNotesCubit extends Cubit<CoachNotesState> {
  CoachNotesCubit() : super(CoachNotesInitial());

  static CoachNotesCubit get(context) => BlocProvider.of(context);

  late final Exercise exercise ;

  List<NoteModel> notes = [];
  void init({required Exercise exercise}) {
    this.exercise = exercise;

    getAllNotes();
  }
  Future getAllNotes()async{
    notes = [];
    emit(CoachNotesLoading());
    await NotesController.getAllNotes(exerciseId: exercise.exerciseId).catchError((e){
      emit(CoachNotesError());
      return null;
    }).then((value){
      if(value != null && value.state) {
        notes = List<NoteModel>.generate(value.data.length, (index) => NoteModel.fromJson(value.data[index]));
        notes= notes.reversed.toList();
        emit(CoachNotesDone());
      }else if(value != null){
        showMessage(value.message);
        emit(CoachNotesError());
      }else{
        if (kDebugMode) {
          print('value is null');
        }
      }
    });
  }


}
